import React from "react";

import { Select, Badge } from  'antd';

import {fetchData,getListDataMap,setStoreItem,getStoreItem} from '../../common';

import UIIcon from '../../uicomponents/UIIcon';

const { Option, OptGroup } = Select;
/**
 * 选择产品版本
 */
export default class ProductVersionSelect extends React.Component {

	constructor(props) {
		super(props);

		this.state = {
			productVersions: fetchData('PRODUCT/REQ/ProductDefine/getProductVersionTreeSelect', {}),
			versionStatusMap: getListDataMap('ProductUI:product.productVersionStatus'),
			versionTypeMap: getListDataMap('ProductUI:product.versionType'),
		}
	}

	renderVersionNode = (node) => {
		const { versionStatusMap, versionTypeMap } = this.state;

		let color = "";
		let status = versionStatusMap[node.nodeData.status].text;
		let type = versionTypeMap[node.nodeData.versionType].text;
		if ('ACTIVED' === node.nodeData.status) {
			color = 'processing'
		}
		else if ('PLANED' === node.nodeData.status) {
			color = 'success'
		}
		else if ('CLOSED' === node.nodeData.status) {
			color = 'default'
		}
		else if ('RELEASED' === node.nodeData.status) {
			color = 'warning'
		}
		return (<span><UIIcon icon="tag" /><Badge status={color} style={{ marginLeft: 6 }} text={node.text + ' ' + status + '(' + type + ')'} /></span>)
	}

	handleSelect = (value, opt) => {
		const { productVersions } = this.state;
		productVersions.map(product => {
			product.nodes.map(version => {
				if (version.id === value) {
					if (this.props.onSelectProduct) {
						this.props.onSelectProduct(version.nodeData);
						setStoreItem('lastSelectProductVersionId', value);
					}
				}
			});
		});
	}

	render() {
		const { productVersions } = this.state;
		return (
			<Select style={{ width: 250 }}

				onSelect={this.handleSelect}
				placeholder="请选择产品版本"
				defaultValue={getStoreItem('lastSelectProductVersionId')}
			>
				{productVersions.map(product => (
					<OptGroup label={<span><UIIcon icon="suitcase"/>{product.text}</span>} key={product.text}>
						{product.nodes.map(version => (
							<Option value={version.id} key={product.text + version.id}>{this.renderVersionNode(version)}</Option>
						))}
					</OptGroup>
				))}
			</Select>
		);
	}
}